<template>
  <el-dialog class="salesmanDialog" :visible.sync="visible" :title="dataForm.userId?'编辑':'新增'" width="500px" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submit()" label-width="120px">
      <el-form-item label="人员名称" prop="username">
        <el-input class="s_input" v-model="dataForm.username"></el-input>
      </el-form-item>
      <el-form-item label="人员密码" prop="password">
        <el-input class="s_input" v-model="dataForm.password"></el-input>
      </el-form-item>
      <el-form-item label="人员邮箱" prop="email">
        <el-input class="s_input" v-model="dataForm.email"></el-input>
      </el-form-item>
      <el-form-item label="人员手机" prop="mobile">
        <el-input class="s_input" v-model="dataForm.mobile"></el-input>
      </el-form-item>
      <el-form-item label="所属部门" prop="roleIdList">
        <el-select class="s_input" v-model="dataForm.roleIdList">
          <el-option v-for="(item, index) in roleList" :key="index" :label="item.roleName" :value="item.roleId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small" type="primary" @click="submit()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {},
      roleList: []
    }
  },
  computed: {
    dataRule () {
      return {
        username: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        password: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        email: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        roleIdList: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 显示弹窗
    init (row) {
      this.visible = true
      this.getroleList()
      if (row) {
        this.dataForm = JSON.parse(JSON.stringify(row))
        this.dataForm.password = ''
        this.dataForm.roleIdList = this.dataForm.roleIdList[0]
      } else {
        this.dataForm = {
          userId: '',
          username: '',
          password: '',
          email: '',
          mobile: '',
          roleIdList: '',
          roleName: '',
          status: 1
        }
      }
      console.log(this.dataForm)
    },
    // 表单提交
    submit: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        let dataForm = JSON.parse(JSON.stringify(this.dataForm))
        dataForm.roleName = this.roleList.find(item => item.roleId === dataForm.roleIdList).roleName
        dataForm.roleIdList = [dataForm.roleIdList]
        this.$http.post('/sys/user/' + (dataForm.userId ? 'update' : 'save'), dataForm).then(({ data: res }) => {
          if (res.code === 0) {
            this.$message.success('保存成功!')
            this.visible = false
            this.$emit('refresh')
          } else {
            this.$message.error(res.msg)
          }
        }).catch((error) => {
          return this.$message.error(error)
        })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    // 获取部门数据
    getroleList () {
      this.$http.get('/sys/role/select').then(({ data: res }) => {
        let { code, msg, list } = res
        console.log(code, msg, list)
        this.roleList = []
        if (code === 0 && list) {
          this.roleList = list && list.length ? list : []
        }
        if (code !== 0) {
          this.$message.error(msg)
        }
      })
    }
  }
}
</script>
<style lang="scss">
.salesmanDialog{
  .el-dialog__header{
    .el-dialog__title{
      font-size: 14px;
      font-weight: bold;
    }
    border-bottom: 1px solid #f2f2f2;
  }
  .el-dialog__body{
    padding: 15px 20px;
  }
  .s_input{
    width: 80%;
    margin-right: 10px;
  }
}
</style>
